#include <amqpcpp.h>
#include <amqpcpp/libev.h>

// 消息回调处理函数的实现
void MessageCb(AMQP::TcpChannel *channel, const AMQP::Message &message, uint64_t deliveryTag, bool redelivered)
{
    std::string msg(message.body(), message.bodySize());
    std::cout << msg << std::endl;
    channel->ack(deliveryTag); // 对消息进行确认
}
int main()
{
    auto *loop = EV_DEFAULT;
    AMQP::LibEvHandler handler(loop);

    AMQP::Address address("amqp://root:123456@127.0.0.1:5672/");
    AMQP::TcpConnection connection(&handler, address);

    AMQP::TcpChannel channel(&connection);
    // 声明交换机
    channel.declareExchange("test-exchange", AMQP::ExchangeType::direct)
        .onError([](const char *message)
                 {
                        std::cout << "声明交换机失败：" << message << std::endl;
            exit(0); })
        .onSuccess([]()
                   { std::cout << "test-exchange 交换机创建成功！" << std::endl; });

    // 声明队列
    channel.declareQueue("test-queue")
        .onError([](const char *message)
                 {
            std::cout << "声明队列失败：" << message << std::endl;
            exit(0); })
        .onSuccess([]()
                   { std::cout << "test-queue 队列创建成功！" << std::endl; });

    // 针对交换机绑定队列
    channel.bindQueue("test-exchange", "test-queue", "test-queue-key")
        .onError([](const char *message)
                 {
            std::cout << "test-exchange - test-queue 绑定失败：" << message << std::endl;
            exit(0); })
        .onSuccess([]()
                   { std::cout << "test-exchange - test-queue 绑定成功！" << std::endl; });
    // 向交换机发布消息
    for (int i = 0; i < 10; i++)
    {
        std::string msg = "Hello Bite-" + std::to_string(i);
        bool ret = channel.publish("test-exchange", "test-queue-key", msg);
        if (ret == false)
        {
            std::cout << "publish 失败！\n";
        }
    }
    // 启动底层网络通信框架--开启I/O
    ev_run(loop, 0);
    return 0;
}